package cn.zjj.order.controller;

import cn.zjj.basic.anno.PreAuthorize;
import cn.zjj.basic.util.JsonResult;
import cn.zjj.basic.util.PageList;
import cn.zjj.order.domain.OrderPetAcquisition;
import cn.zjj.order.dto.OrderDto;
import cn.zjj.order.query.OrderPetAcquisitionQuery;
import cn.zjj.order.service.IOrderPetAcquisitionService;
import cn.zjj.user.domain.Logininfo;
import cn.zjj.user.util.LoginContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/acquisitionOrder")
@Api(value = "收购订单的API",description="收购订单相关的CRUD功能")
public class OrderPetAcquisitionController {

    @Resource
    private IOrderPetAcquisitionService orderPetAcquisitionService;


    //查询所有
    @GetMapping
    @ApiOperation(value = "查询所有数据" )
//    @PreAuthorize(name = "收购订单列表",value = "orderPetAcquisition:list")
    public List<OrderPetAcquisition> selectAll(){

        return orderPetAcquisitionService.selectAll();
    }

    //查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "查询一条数据" )
//    @PreAuthorize(name = "查询收购订单",value = "orderPetAcquisition:get")
    public OrderPetAcquisition selectById(@PathVariable("id") Long id){
        return orderPetAcquisitionService.selectById(id);
    }



    //添加或修改
    @PutMapping
    @ApiOperation(value = "添加或修改数据" )
//    @PreAuthorize(name = "收购订单保存",value = "orderPetAcquisition:save")
    public JsonResult updateOrAdd(@RequestBody OrderPetAcquisition orderPetAcquisition){
        try {
            if(orderPetAcquisition.getId()==null){
                orderPetAcquisitionService.insert(orderPetAcquisition);
            }else {
                orderPetAcquisitionService.update(orderPetAcquisition);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询或高级查询" )
    public PageList<OrderPetAcquisition> queryPage(@RequestBody OrderPetAcquisitionQuery query, HttpServletRequest request){
        Logininfo logininfo = LoginContext.getLogininfo(request);
        return orderPetAcquisitionService.selectQuery(query,logininfo.getId());
    }

    @PatchMapping
    @ApiOperation(value = "批量删除" )
//    @PreAuthorize(name = "收购订单批量删除",value = "orderPetAcquisition:patchDel")
    public JsonResult delete(@RequestBody List<Long> ids){
        try {
            orderPetAcquisitionService.patchDel(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

    //删除单个
    @DeleteMapping("/{id}")
    @ApiOperation(value = "订单取消" )
//    @PreAuthorize(name = "删除收购订单",value = "orderPetAcquisition:delete")
    public JsonResult cancel(@PathVariable("id") Long id){
        try {
            orderPetAcquisitionService.cancel(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }


    //分页和高级查询
    @PostMapping("/confirm")
    @ApiOperation(value = "订单确认" )
    public JsonResult confirm(@RequestBody OrderDto dto){
        try {
            orderPetAcquisitionService.confirm(dto);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me(false,"操作失败");
        }
    }

}
